package cn.iocoder.yudao.module.agent.dal.mysql.brokerageuser;

import java.util.*;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokerageuser.AgentBrokerageUserDO;
import cn.iocoder.yudao.module.agent.dal.dataobject.user.AgentUserDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.agent.controller.admin.brokerageuser.vo.*;

/**
 * 代理人分销用户 Mapper
 *
 * @author 曹建伟
 */
@Mapper
public interface AgentBrokerageUserMapper extends BaseMapperX<AgentBrokerageUserDO> {

    default PageResult<AgentBrokerageUserDO> selectPage(AgentBrokerageUserPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AgentBrokerageUserDO>()
                .eqIfPresent(AgentBrokerageUserDO::getUserId, reqVO.getUserId())
                .eqIfPresent(AgentBrokerageUserDO::getAgentUserId, reqVO.getAgentUserId())
                .betweenIfPresent(AgentBrokerageUserDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AgentBrokerageUserDO::getId));
    }

    default List<AgentBrokerageUserDO> selectList(AgentBrokerageUserExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<AgentBrokerageUserDO>()
                .eqIfPresent(AgentBrokerageUserDO::getUserId, reqVO.getUserId())
                .eqIfPresent(AgentBrokerageUserDO::getAgentUserId, reqVO.getAgentUserId())
                .betweenIfPresent(AgentBrokerageUserDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AgentBrokerageUserDO::getId));
    }

    default int updateOrderCount(Long userId, Integer count, Integer price) {
        LambdaUpdateWrapper<AgentBrokerageUserDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<AgentBrokerageUserDO>()
                .setSql(" order_count = order_count + " + count +
                        ", total_price = total_price + " + price)
                .eq(AgentBrokerageUserDO::getUserId, userId);

        return update(null, lambdaUpdateWrapper);
    }

}
